﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using HF.BaseWebCtrlLib;
using HF.Business.Common;
using HF.Framework;
using HF.WorkFlow.Const;
using HF.WorkFlow.DataContract;
using HF.WorkFlow.Engine;

public partial class UserControls_Affix : BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load();
        if (!IsPostBack)
        InitData();
    }
    private void InitData()
    {
        DataTable dt = AffixFiles.GetAffixTable(WorkflowInsId);
        gvAffixfiles.DataSource = dt;
        gvAffixfiles.DataBind();
    }
    protected void ButtonConfirm_Click(object sender, EventArgs e)
    {
        //检查上传文件不为空

        if (inputFile.PostedFile.ContentLength > 0)
        {
            //设定上传文件的保存路径
            string strSaveDir = "../Files/";
            string strName = this.inputFile.PostedFile.FileName;
            //取得文件名(抱括路径)里最后一个"."的索引
            int intExt = strName.LastIndexOf(".");
            //取得文件扩展名
            string strExt = strName.Substring(intExt);
            //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复

            //取得文件名(包括路径)里最后一个"\"的索引
            int intPath = strName.LastIndexOf("\\");
            //取得文件名(不包括路径)
            string strNewName = strName.Substring(intPath+1);
            string filenameid = Guid.NewGuid().ToString();
            //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
            //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
            string filenamepath = Server.MapPath(strSaveDir + filenameid + strExt);
            this.inputFile.PostedFile.SaveAs(filenamepath);
            //得到这个文件的相关属性:文件名,文件类型,文件大小
            labelUpResult.Text = "上传成功！";
            //labelFileName.Text = "文件源：" + strName;
            //labelFileExt.Text = "文件类型：" + inputFile.PostedFile.ContentType + "( " + strExt + " )";
            //labelFileSize.Text = "文件大小：" + (inputFile.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
            AffixFiles affix = new AffixFiles();
            affix.AffixId = filenameid;
            affix.WorkflowId = WorkflowId;
            affix.WorkflowInsId = WorkflowInsId;
            affix.WorktaskId = WorktaskId;
            affix.WorktaskInsId = WorktaskInsId;
            affix.FileNameId = filenameid + strExt;
            affix.FileName = strNewName;
            affix.FilePath = filenamepath;
            affix.InsertAffix();
            labelUpResult.Text = "上传成功！";
            InitData();
        }
    }
    public static void OpenWindowInNewPage(Page curPage ,string destUrl)
         {            
            string scriptString = string.Format("<script language='JavaScript'>window.open('" + "{0}" + "','_new');<" ,destUrl) ;
             scriptString += "/";
            scriptString += "script>";
            if(!curPage.IsStartupScriptRegistered("Startup"))
             {
                curPage.RegisterStartupScript("Startup", scriptString);
            }
       }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string parms = (sender as LinkButton).CommandArgument.ToString();
        OpenWindowInNewPage(this.Page, @"../Files/" + parms);
        
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        if (CtrlState == WorkFlowConst.STATE_VIEW)
            return;
        string[] parms = (sender as LinkButton).CommandArgument.Split(',');
        AffixFiles.DeleteAffix(parms[0]);//删除数据库记录文件
        File.Delete(parms[1]);
        InitData();

    }
    protected void gvAffixfiles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       

    }
}
